package cn.lesper.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//基本连接池
//原生jdbc
//未来规划:抽象可继承
public abstract class ExtendBase extends Nature{

    //配置在yaml语言中,进行灵活读取
    public String DRIVER = DB_DRIVER;
    public String URL = DB_URL;
    public String USER = DB_USER;
    public String PASSWORD = DB_PWD;

    /**
     * @note 创建数据库连接对象
     * @Exception ClassNotFoundException,SQLException
     * @return connection
     */
    public Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * @note 释放数据库资源对象
     * @Exception SQLException
     */
    public void closeAll(ResultSet rs, PreparedStatement pstmt, Connection conn) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (pstmt != null) {
                pstmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
